import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xin_he/utils/xin_he_toast.dart';

import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_event_bus.dart';
import '../../../utils/xin_he_storage.dart';
import '../../home/model/xin_he_goods_item_entity.dart';
import '../../home/model/xin_he_special_distract_entity.dart';
import '../../shopping_car/models/xin_he_goods_model.dart';
import '../../shopping_car/xin_he_shopping_car/xin_he_shopping_car_logic.dart';
import 'xin_he_category_state.dart';

class XinHeCategoryLogic extends GetxController {
  final XinHeCategoryState state = XinHeCategoryState();

  RefreshController refreshController = RefreshController(initialRefresh: false);
  RxInt selectIndex = 0.obs;
  RxList leftCategoryList = ['热门回购','新鲜水果','生鲜菜品','粮油菜品','日常用品','新鲜水果','生鲜菜品','粮油菜品','日常用品','新鲜水果','生鲜菜品','粮油菜品','日常用品','新鲜水果','生鲜菜品','粮油菜品',].obs;
  RxList rightCategoryList = [].obs;

  RefreshController rightFreshController = RefreshController(initialRefresh: false);

  // RxInt tabIndex = 0.obs;
  // RxList tabsList = [].obs;
  // RxList itemsList = [].obs;
  RxInt  pageIndex = 0.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    bus.on(XinHeStorage.KNeedRefresh, (arg) async {

      if(arg == 1) {
       await   requestAllTabData();
      }

    });

  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();

    requestAllTabData();

  }

  ///下拉刷新
  void onRefresh() {
    // _requestLeftCategoryData();
  }

  void changeIndex(index, id) {

    XinHeSpecialDistractEntity model = state.allTabList[index];
    requestAllTabsDataItem(model.id);
    selectIndex.value = index;
    update();
  }

  ///添加商品进购物车
  void addGoodsToCartAction(XinHeGoodsModel model){

    XinHeToast.center('已添加');
    final allLogic = Get.find<XinHeShoppingCarLogic>();
    allLogic.state.allItemsList.add(model);

  }


  ///all tab
  requestAllTabData()async{
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/goodsType/queryGoodsTypeList',isPost: false);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }
    List list = result.data['data'] ??[];
    List<XinHeSpecialDistractEntity> items = [];

    if(list.isNotEmpty){
      for(var e in list){
        XinHeSpecialDistractEntity model = XinHeSpecialDistractEntity.fromJson(e);
        items.add(model);
      }

      // categoryTabController =
      //     TabController(vsync: this, length: items.length, initialIndex: 0);
      XinHeSpecialDistractEntity model = items[0];

      requestAllTabsDataItem(model.id);

      state.allTabList.value = items;

    }



  }

  ///all tab data
  requestAllTabsDataItem(tabId)async{

    Map<String,dynamic> ss = {
      'goodsTypeId':'${tabId}'
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/goodsType/queryGoodsTypeGoods',params: ss,isPost: false);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }


    List list = result.data['data'] ??[];
    List <XinHeGoodsItemEntity>items = [];

    if(list.isNotEmpty){
      for(var e in list){
        XinHeGoodsItemEntity model = XinHeGoodsItemEntity.fromJson(e);
        // model.iconPath = 'https://xhyp.quexiaoer.cn/upload/goods-photo/goods-photo_20230703074806830_3210.png';
        items.add(model);
      }
      state.allTabDataList.value = items;
    }else {
      state.allTabDataList.clear();

    }
  }


}
